package com.ly.common.util;

import com.google.gson.Gson;
import com.ly.common.biz.BizException;
import com.ly.common.bean.Result;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Servlet 工具类
 */
public class ActionUtils {
    /**
     * Servlet 调用业务方法的模板函数
     *
     * @param successMsg 执行成功时, 返回的消息
     * @param handler    不返回结果对象
     * @throws IOException
     */
    public static Result exec(String successMsg,
                              ResultHandler handler) throws IOException {
        Result result;
        try {
            handler.handle();
            result = new Result(1, successMsg, null);
        } catch (BizException e) {
            e.printStackTrace();
            result = new Result(0, e.getMessage(), null);
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(0, "系统异常, 请联系管理员!", null);
        }
        return result;
    }

    /**
     * Servlet 调用业务方法的模板函数
     *
     * @param successMsg 执行成功时, 返回的消息
     * @param handler    返回结果对象
     * @throws IOException
     */
    public static Result exec(String successMsg,
                              ResultHandlerWithReturn handler) throws IOException {
        Result result;
        try {
            Object res = handler.handle();
            result = new Result(1, successMsg, res);
        } catch (BizException e) {
            e.printStackTrace();
            result = new Result(0, e.getMessage(), null);
        } catch (Exception e) {
            e.printStackTrace();
            result = new Result(0, "系统异常, 请联系管理员!", null);
        }
        return result;
    }

    /**
     * Servlet 调用业务方法的模板函数
     *
     * @param successMsg 执行成功时, 返回的消息
     * @param out
     * @param handler    不返回结果对象
     * @throws IOException
     */
    public static void exec(String successMsg,
                            PrintWriter out,
                            ResultHandler handler) throws IOException {
        Result result = exec(successMsg, handler);
        out.append(new Gson().toJson(result));
    }

    /**
     * Servlet 调用业务方法的模板函数
     *
     * @param successMsg 执行成功时, 返回的消息
     * @param out
     * @param handler    不返回结果对象
     * @throws IOException
     */
    public static void exec(String successMsg,
                            PrintWriter out,
                            ResultHandlerWithReturn handler) throws IOException {
        Result result = exec(successMsg, handler);
        out.append(new Gson().toJson(result));
    }

    /**
     * Servlet 调用业务方法的模板函数
     *
     * @param successMsg 执行成功时, 返回的消息
     * @param response
     * @param handler    不返回结果对象
     * @throws IOException
     */
    public static void exec(String successMsg,
                            HttpServletResponse response,
                            ResultHandler handler) throws IOException {
        exec(successMsg, response.getWriter(), handler);
    }

    /**
     * Servlet 调用业务方法的模板函数
     *
     * @param successMsg 执行成功时, 返回的消息
     * @param response
     * @param handler    返回结果对象
     * @throws IOException
     */
    public static void exec(String successMsg,
                            HttpServletResponse response,
                            ResultHandlerWithReturn handler) throws IOException {
        exec(successMsg, response.getWriter(), handler);
    }

    // 回调接口 ( 业务方法不返回结果 ) => 函数接口
    public static interface ResultHandler {
        void handle() throws BizException;
    }

    // 回调接口 ( 业务方法返回结果 )  => 函数接口
    public static interface ResultHandlerWithReturn {
        Object handle() throws BizException;
    }

    /**
     * 判断是否是 手机终端
     *
     * @param userAgent
     * @return
     */
    public static boolean isMobileDevice(String userAgent) {
        String[] deviceArray = new String[]{"android", "mac os", "windows phone"};
        if (userAgent == null)
            return false;
        userAgent = userAgent.toLowerCase();
        for (int i = 0; i < deviceArray.length; i++) {
            if (userAgent.indexOf(deviceArray[i]) > 0) {
                return true;
            }
        }
        return false;
    }

}


